package com.honey.compilation.sql.dml;

import com.honey.compilation.sql.SqlColumn;
import com.honey.compilation.sql.SqlKeyWord;
import com.honey.compilation.sql.SqlTable;
import com.honey.compilation.sql.SqlType;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.CompilationException;
import com.honey.core.utils.StringUtility;

public class SelectColumn extends SqlColumn{

	/** 列所属表的别名 */
	private String alias;
	
	/** 列所属的表*/
	private SqlTable table ;

	/** 是否自动别名*/
	private boolean isAlias = true;
	
	/**
	 * 构造函数
	 * @param table 表
	 * @param name 列
	 */
	public SelectColumn(SqlTable table, String name ) {
		super(name);
		this.table = table;
	}
	
	public SelectColumn(String schema,String table, String name ) {
		super(name);
		this.table = new SqlTable(schema,table);
	}
	
	/**
	 * 构造函数
	 * @param name 列名称
	 */
	public SelectColumn(String name ) {
		super(name);
	}
	
	/**
	 * 构造函数
	 * @param name 
	 * @param type
	 */
	public SelectColumn(String name ,SqlType type) {
		super(name,type);
	}
	
	/**
	 * 构造函数
	 * @param name
	 * @param type
	 */
	public SelectColumn(String name ,int type) {
		super(name,type);
	}
	
	/**
	 * 
	 * @param name 列名
	 * @param alias 列所属表的别名
	 */
	public SelectColumn(String name, String alias ) {
		super(name);
		this.alias = alias;
	}
	
	/**
	 * table name
	 * @return
	 */
	public String getTableName() {
		if(table == null)
			return null;
		return table.getTableName();
	}

	public SqlTable getTable() {
		return table;
	}
	
	public void setTable(SqlTable table) {
		this.table = table;
	}
	/**
	 * table name
	 * @param tableName
	 */
	public void setTableName(String tableName) {
		this.table =new SqlTable( tableName );
	}

	public String getAlias() {
		return alias;
	}
	
	public void setAlias(String alias) {
		this.alias = alias;
	}
	
	void setEnableAlias(boolean b) {
		this.isAlias = b;
	}

	@Override
	public CharacterBuilder compiledContent(int indentLevel) throws CompilationException {
		CharacterBuilder answer = CharacterBuilderFactory.createC16StringBuilder();
		if(table != null && StringUtility.stringHasValue(table.getTableName())){
			answer.append( table.compiledContent(0, false))
			.append(SqlKeyWord.OPERATION_PERIOD)
			;
		}
		answer.append(getName());
		if( StringUtility.stringHasValue( alias) && isAlias){
			answer
			.append(SqlKeyWord.AS)
			.append(alias)
			.append(SqlKeyWord.OPERATION_SPACE);
			;
		}
		return answer;
	}

	
	/**
	 * 数据库的列名是不能重复的,所以只要列名相等就是同一个列.
	 */
	@Override
	public boolean equals(Object obj) {
		if( obj instanceof SqlColumn ){
			boolean b=false;
			SelectColumn column =(SelectColumn)obj;
			if(this.table==null && column.table == null){
				b=true;
			}else if(this.table==null && column.table != null){
				b=false;
			}else{
				b = this.table.equals(column.table); 
			}
			return b&super.equals(obj);
		}else{
			return super.equals(obj);
		}
		
//		if( obj instanceof SqlColumn ){
//			SelectColumn column =(SelectColumn)obj;
//			boolean b =this.getName().equals(column.getName()); 
//			return b;
//		}else{
//			return false;
//		}
	}

	/**
	 * 数据库的列名是不能重复的,所以只要列名相等就是同一个列.
	 */
	@Override
	public int hashCode() {
		String s = "";
		if(table!= null){
			s=String.valueOf(table.hashCode());
		}
		s =s+ getName().getValue();
		return s.hashCode();
		
	}
}
